package gui.table;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.event.TableModelListener;
import javax.swing.table.*;

import api.Person;

/**
 * @author Ann Raboschuk
 */
public class CustomTableModel extends AbstractTableModel
{
   private static final long serialVersionUID = 1L;
   private static String[] columNames = {"ID", "Surname", "Name", "Age", "Sex", "E-mail", "Phone", "Address"};
   private Set<TableModelListener> listeners = new HashSet<TableModelListener>();
   private List<Person> people;

   public CustomTableModel(List<Person> persons)
   {
      this.people = persons;
   }
   
   public List<Person> getPersons()
   {
      return people;
   }

   public void addTableModelListener(TableModelListener listener)
   {
      listeners.add(listener);
   }

   public Class<?> getColumnClass(int columnIndex)
   {
      Class<?> type = Object.class;
      switch (columnIndex)
      {
      case 0:
         type = Long.class;
         break;
      case 1:
      case 2:
      case 5:
      case 6:
      case 7:
         type = String.class;
         break;
      case 3:
         type = Integer.class;
         break;
      case 4:
         type = Character.class;
         break;
      }
      return type;
   }

   public int getColumnCount()
   {
      return 8;
   }

   public String getColumnName(int columnIndex)
   {
      return columNames[columnIndex];
   }

   public int getRowCount()
   {
      return people.size();
   }

   public Object getValueAt(int rowIndex, int columnIndex)
   {
      Person person = people.get(rowIndex);
      return getColumnValue(person, columnIndex);
   }
   
   protected Object getColumnValue(Person person, int columnIndex)
   {
      switch (columnIndex)
      {
      case 0:
         return person.getId();
      case 1:
         return person.getSurname();
      case 2:
         return person.getName();
      case 3:
         return person.getAge();
      case 4:
         return person.getSex();
      case 5:
         return person.getEmail();
      case 6:
         return person.getPhone();
      case 7:
         return person.getAddress();
      }
      return "";
   }

   public boolean isCellEditable(int rowIndex, int columnIndex)
   {
      return false;
   }

   public void removeTableModelListener(TableModelListener listener)
   {
      listeners.remove(listener);
   }
   
   public class ColumnSorter implements Comparator<Person>
   {
      private int columnIndex;
      private boolean ascending;

      ColumnSorter(int colIndex, boolean ascending)
      {
         this.columnIndex = colIndex;
         this.ascending = ascending;
      }

      @SuppressWarnings({ "unchecked", "rawtypes" })
      public int compare(Person person1, Person person2)
      {
         if (person1 == null && person2 == null)
            return 0;
         if (person1 == null)
            return 1;
         if (person2 == null)            
            return -1;
         
         int result = 0;
         Comparable value1 = (Comparable) CustomTableModel.this.getColumnValue(person1, columnIndex);
         Comparable value2 = (Comparable) CustomTableModel.this.getColumnValue(person2, columnIndex);
         if (ascending)
            result = value1.compareTo(value2);
         else
            result = value2.compareTo(value1);
         return result;
      }
   }
}
